package com.ias.agdis_r.netservice;

import android.os.Handler;

import com.ias.agdis_r.activity.LuggageBarCodeActivity;
import com.ias.agdis_r.bean.task.LuggageBarCode;
import com.ias.agdis_r.db.BoxDBHelper;
import com.ias.agdis_r.manager.ActionManager;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.request.UploadLuggageRequest;
import com.ias.agdis_r.util.network.HttpUtil;

import java.util.List;

public class SendLuggageBarCodeThread extends Thread {

    private Handler handler;

    public SendLuggageBarCodeThread(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        BoxDBHelper boxDBHelper = new BoxDBHelper(DataBaseManager.getInstance().getContext());
        List<LuggageBarCode> list = boxDBHelper.getSendLuggageBarCode();
        if (list != null && list.size() > 0) {
            boxDBHelper.setMultiLuggageBarCodeState(list, LuggageBarCode.STATE_SENDING);
            ActionManager.getInstance().updateUI(LuggageBarCodeActivity.refreshTabUploading);
            UploadLuggageRequest uploadLuggageRequest = new UploadLuggageRequest();
            uploadLuggageRequest.setLuggagesList(list);
            HttpUtil.getInstance().sendRequest(uploadLuggageRequest, MessageModel.ULBC, false);
        }
        boxDBHelper.closeDB();
        handler.postDelayed(this, 60000);
    }
}
